/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */


var dataID = $.getUrlVar("id");
var forword = encodeURIComponent(window.location.href);
var backUrl = $.getUrlVar("back");

/*Vue.component('modal', {
    template: '#modal-template',
    props: {
        show: {
            type: Boolean,
            replace: true,
            required: true,
            twoWay: true
        },
        users: [],
        userList: '',
        query: ''
    },
    created: function() {

    },
    compiled: function() {
        var _self = this;
        _self.loadlist('');
    },
    methods: {
        checked: function(event) {
            //选择
            if ($(event.currentTarget).hasClass('on')) {
                $(event.currentTarget).removeClass('on');
            } else {
                $(event.currentTarget).addClass('on');

            }
        },
        ok: function() {
            //确定
            var _self = this;
            var arr = [];
            $('#uluser li.on').each(function() {
                arr.push($(this).attr('dataid'));
            });
            if (arr.length > 0) {
                _self.userList = arr.join(',');
                _self.$parent.showModal = false;
                _self.$parent.shareReg(_self.userList);
            } else {
                alert("请选择");
            }
        },
        cancel: function() {
            var _self = this;
            _self.$parent.showModal = false;
        },
        search: function() {
            //搜索
            var val = $("#searchword").val();
            if (val == "") {
                this.query = '';
                return;
            }
            var _self = this;
            _self.loadlist(val);
        },
        loadlist: function(uname) {
            //列表
            var _self = this;
            var udata = {
                uname: uname
            };
            udata = Common.jsonEncode(udata);
            Common.ajaxCall({ url: 'look_getShareUsers', datatype: 'json', type: 'get', data: udata }, function(data) {
                if (data.error_code == 0) {
                    var obj = data.data;
                    _self.users = obj;
                } else {
                    alert(data.msg);
                }
            });
        }
    }
})*/

var info = new Vue({
    el: "#app",
    data: {
        scrollbar: {},
        data: [],
        isLoading: true,
        pageIndex: 1,
        pageSize: PAGESIZE,
        info: {},
        AnswerAdopt: {},
        IsAnswerAdopt: 0,
        addShow: false,
        showModal: false,
        isCollection: 0, //
        isFollow: 0, //
        reqOut: false, //判断请求数据是否已完
        shareObj:{},
        selfId:''
    },
    created: function() {
        var dtd = $.Deferred();
        var _self = this;
        //评论列表
        var data = {
            questionID: dataID,
            pageSize: _self.pageSize,
            pageIndex: _self.pageIndex
        };
        data = Common.jsonEncode(data);
        _self.scrollbar = new scrollBar({
            isLoading: _self.isLoading,
            url: "ask_getAnswerList",
            type: "get",
            data: data
        });

        _self.loadlist();


        //问题详情
        var data = {
            questionID: dataID
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'ask_getQuestionDetail', datatype: 'json', type: 'get', data: data }, function(data) {
            if (data.error_code == 0) {
                var obj = data.data[0];
                _self.info = obj;
                _self.isCollection = obj.isCollection;
                _self.isFollow = obj.isFollow;
                
            } else {
                alert(data.msg);
            }
        });

        //采纳作答
        var data = {
            questionID: dataID
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'ask_getAnswerAdopt', datatype: 'json', type: 'get', data: data }, function(data) {
            if (data.error_code == 0) {
                var obj = data.data[0];
                _self.AnswerAdopt = obj;

                if ($.isEmptyObject(obj)) {
                    _self.IsAnswerAdopt = 0;
                } else {
                    _self.IsAnswerAdopt = 1;
                }
            } else {
                _self.IsAnswerAdopt = 0;
            }
        });


    },
    compiled: function() {
        var _self = this;
        $(document).scroll(function() {
            var docHeight = $(document).height(),
                winHeight = $(window).height();
            if ((docHeight > winHeight)&&($(document).scrollTop() >= docHeight - winHeight)) {
                if (!_self.reqOut) {
                    _self.reqOut = true;
                    var str = '<img class="loading-img" src="images/loading.gif"></img>';
                    myLoad.pullLoad('quesCont', str);
                    _self.loadlist(_self.tabs);

                }
            }
        });

        // 比例
        myJS.imgScale('.j_6', '6', '5');
        myJS.ImgFull('.loadImg', 'images/liby.png');
    },
    methods: {
        loadlist: function(callback) {
            var _self = this;

            _self.scrollbar.load(function(pageIndex, data) {
                for (var i = 0; i < data.length; i++) {
                    _self.data.push(data[i]);
                }
                _self.reqOut = false;

                //实际数据与请求的数据量比较
                if (data.length >= _self.pageSize) {
                    myLoad.pullHide(); //加载后，把效果去掉
                } else {
                    _self.reqOut = true;
                    myLoad.noDate(); //暂无数据
                    _self.isLoading = false;
                }
                _self.pageIndex = pageIndex;
            }, function(isLoading) {
                _self.isLoading = isLoading;
                _self.reqOut = true;
            });

        },
        addComment: function(id) {
            window.location.href = "chatView.html?id=" + id + "&aid=" + dataID + "&back=" + forword;
        },
        toHisPage:function(authorID){
            if(authorID == this.selfId){
                window.location.href = "myUserCenter.html?" + "&back=" + forword;
            }else{
                window.location.href = "userCenter.html?uid=" + authorID + "&back=" + forword;
            }
        },
        addAnswer: function (id) {
            if (this.info.isClose == 1) {
                return;
            }
            window.location.href = "answer.html?id=" + id + "&back=" + forword;
        },
        back: function() {
            window.location.href = decodeURIComponent(backUrl);
        },
        add: function() {
            var _self = this;
            if (this.addShow == false) {
                this.addShow = true;
                Common.ajaxCall({ url: 'user_getUserInfo', datatype: 'json', type: 'get' }, function(data) {
                    if (data.error_code == 0) {
                        _self.selfId = data.data.uid;
                        
                    } else {
                        alert(data.msg);
                    }
                });
            } else {
                this.addShow = false;
            }
        },
        addLike: function(answerID,e) {
            // 点赞
            var data = {
                answerID: answerID
            };
            data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'ask_addLike', datatype: 'json', type: 'get', data: data }, function(data) {
                var likeCount = parseInt($(e.target).html())+1;
                $(e.target).html(likeCount);
                alert(data.msg);
            });
        },
        addAdopt: function(answerID) {
            // 问题采纳
            alert({
                close: false,
                text: '是否采纳小白的回答？',
                kind: 2,
                okEvent: function() {
                    var data = {
                        questionID: dataID,
                        answerID: answerID
                    };
                    data = Common.jsonEncode(data);
                    Common.ajaxCall({ url: 'ask_addAdopt', datatype: 'json', type: 'get', data: data }, function (data) {
                        if (data.error_code == 0) {
                            location.reload();
                        }
                        alert(data.msg);
                    });
                }
            });

        },
        //关注
        addFollow: function() {
            var _self = this;
            _self.addShow = false;
            var data = {
                dataID: dataID,
                type: 3,
                isFollow: _self.isFollow == 1 ? 0 : 1, //1关注，0取消关注
            };
            data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'look_addFollow', datatype: 'json', type: 'get', data: data }, function(data) {
                if (data.error_code == 0) {
                    //修改状态值
                    alert(_self.isFollow == 1 ? "取消关注成功" : "关注成功");
                    _self.isFollow = _self.isFollow == 1 ? 0 : 1;
                    /*
                    var obj = data.data[0];
                    var code = obj.code;
                    var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                    alert({
                        text: '<img src="images/' + pic + '"/>',
                        description: obj.str,
                        kind: 0
                    });
                    */
                } else {
                    alert(data.msg);
                }
            });
        },
        //收藏
        Collection: function() {
            var _self = this;
            _self.addShow = false;

            var data = {
                dataID: dataID,
                type: 3,
                isCollection: _self.isCollection == 1 ? 0 : 1, //是否收藏：1是，0否
            };
            data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'look_addCollection', datatype: 'json', type: 'get', data: data }, function(data) {
                if (data.error_code == 0) {
                    //修改状态值
                    alert(_self.isCollection == 1 ? "取消收藏成功" : "收藏成功");
                    _self.isCollection = _self.isCollection == 1 ? 0 : 1;
                    /*
                    var obj = data.data[0];
                    var code = obj.code;
                    var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                    alert({
                        text: '<img src="images/' + pic + '"/>',
                        description: obj.str,
                        kind: 0
                    });
                    */
                } else {
                    alert(data.msg);
                }
            });
        },
        shareReg: function(userList) {
            var _self = this;
            if(!userList){     //如果没选择分享对象回到原来界面
              _self.showModal = false;
              return;
            }
            alert({
                close: false,
                text: '分享',
                description: '<div class="clearfix shareTxt"><span class="j_1 photo"></span><span class="text">' + info.info.title + '</span></div>',
                fillInput: true,
                kind: 2,
                okEvent: function() {
                    var data = {
                        dataID: dataID,
                        userList: userList,
                        comment: _self.inputText,
                        type: 3
                    };
                    data = Common.jsonEncode(data);
                    Common.ajaxCall({ url: 'look_share', datatype: 'json', type: 'get', data: data }, function(data) {
                        if (data.error_code == 0) {
                            /*
                            var obj = data.data[0];
                            var code = obj.code;
                            var pic = code==1?"moneyPig.png":"lebel.png";
                            alert({
                                text: '<img src="images/'+pic+'"/>',
                                description: obj.str,
                                kind: 0
                            });
                            */
                            alert("分享成功");
                        } else {
                            alert(data.msg);
                        }
                    });
                }
            });
        },
        //分享
        share: function(obj,e) {
            var _self = this;
            _self.showModal = true;
            _self.addShow = false;
            $('#uluser li').removeClass("on");
            $("#searchword").val('');
            $(".search-btn").click();
            console.log(obj);
            _self.shareObj = {
              dataID : obj.shareId, //文档id
              shareTit : obj.shareTit, //文档标题
              sharePic : obj.sharePic    //文档图片
            };
        }

    }

});